<?php

class Default_CartController extends Zend_Controller_Action
{

    public function init()
    {
        $option = array(
                'layout'=>'layout',
                'layoutPath'=> APPLICATION_PATH .'/layouts/scripts/default'
        );
        Zend_Layout::startMvc($option);       
//         $cart = new Zend_Session_Namespace("cart");
//         $this->view->cart = $cart;
        $this->model = new Application_Model_Product();
    }

    public function indexAction()
    {
        $cart = new Zend_Session_Namespace("cart");
        if(isset($cart->item)){
            $this->view->cart = $cart;            
        }
        $this->_helper->layout()->disableLayout();
    }

    public function addAction()
    {
        try {
            $product_id = $this->_request->getParam('productid');
            if($product_id){
                $product = $this->model->Get_Product_by_ID($product_id);
                
                if(!empty($product)){                   
                    $cart = new Zend_Session_Namespace("cart");
                    if(isset($cart->item[$product_id])){
                        $cart->item[$product_id]['quantity'] += 1;                       
                    }
                    else{                        
                        $cart->item[$product_id] = array(
                                                          "product"=>$product,
                                                          "quantity"=>1,            
                                                        );                       
                    }
                    $this->_helper->redirector('review', 'cart', 'default');
                }                
                    
            }
        }
        catch (Zend_Exception $ex){
            echo 'Error: '.$ex->getMessage();
        }
    }

    public function removeAction()
    {
        // action body
        try {
            Zend_Session::namespaceUnset("cart");
            $cart=new Zend_Session_Namespace("cart");
            $this->_helper->redirector('index', 'index', 'default');
        }
        catch (Zend_Exception $ex){
            echo $ex;
        }
        
    }

    public function reviewAction()
    {
        $cart = new Zend_Session_Namespace("cart");
        if(isset($cart->item)){
            $this->view->cart = $cart;
            
        }
        $this->view->headTitle('Giỏ hàng');
        echo 'aaaa';
    }

    public function listAction()
    {
        $cart = new Zend_Session_Namespace("cart");
        if(isset($cart->item)){
            $this->view->cart = $cart;
        }
        $this->_helper->layout()->disableLayout();
    }

    public function delAction()
    {
        try{
            $id = $this->_request->getparam("id");
            $cart = new Zend_Session_Namespace("cart");
            if($id){
                if($cart->item[$id]){
                    unset($cart->item[$id]);
                }
            }
            $this->_helper->layout()->disableLayout();
        }
        catch (Zend_Exception $ex){
            echo $ex;
        }
    }

    public function updateAction()
    {
        $cart = new Zend_Session_Namespace("cart");
        if($this->_request->getParam('num1')){
            $count = 1;
            foreach ($cart->item as $item){
                $num = 'num' .$count;
                $quantity = $this->_request->getParam($num);
                if($quantity < 0)
                    $quantity = 0;
                $cart->item[$item['product']->product_id]['quantity'] = $quantity;
                $count++;           
            }
            
        }
        $this->_helper->redirector('review', 'cart', 'default');
    }
    


}













